GameEngine.cons = {
	PLANE_TYPE_SMALL: Symbol(),
	PLANE_TYPE_MIDDLE: Symbol(),
	PLANE_TYPE_LARGE: Symbol()
}
GameEngine.pause = false;

function GameEngine() {
	var self = this
	self.num=function(num){
		if(num>4){
			return 4
		}
		return num
	}
	self.score = function(val) {
		var span = document.getElementsByClassName("score")[0].children[0]
		if(val != undefined) {
			span.innerText = val;
		} else {
			return Number(span.innerText)
		}
	}
	self.enemes = new Set()
	self.weapons = new Set()
	self.body = document.getElementById("body_main")
	self.start = function(level) {
		//难度等级
		self.difLevel = level;
		//更换界面
		self.body.children[0].style.display = "none"
			//背景移动
		self.bgmoving()
		new Promise(self.loading).then(function() {
			window.myplane = new MyPlane().show()
			self.autoCreateEnemy()
			self.autoCreateWeapon()
		})
		self.num(0)
		return self;
	}
	self.autoCreateWeapon = function() {
		setInterval(function() {
			if(!GameEngine.pause) {
				if(Math.random() < 0.1) {
					self.weapons.add(new Weapon().init().move())
				}
			}
		}, 1000)
	}
	self.autoCreateEnemy = function() {
		setInterval(function() {
			if(!GameEngine.pause) {
				var flag = Math.round(Math.random())
				if(flag) {
					self.enemes.add(new Enemy(GameEngine.cons.PLANE_TYPE_SMALL).init().move())
				}
			}
		}, 500)
		setInterval(function() {
			if(!GameEngine.pause) {
				var flag = Math.round(Math.random())
				if(flag) {
					self.enemes.add(new Enemy(GameEngine.cons.PLANE_TYPE_MIDDLE).init().move())
				}
			}
		}, 2000)
		setInterval(function() {
			if(!GameEngine.pause) {
				var flag = Math.round(Math.random())
				if(flag) {
					self.enemes.add(new Enemy(GameEngine.cons.PLANE_TYPE_LARGE).init().move())
				}
			}
		}, 5000)
	}
	self.loading = function(success) { //加载动画
		//显示logo
		var logo = document.createElement("div")
		logo.className = "logo"
		document.body.appendChild(logo);
		//显示进度条
		var loadDiv = document.createElement("div")
		loadDiv.className = "loading"
		document.body.appendChild(loadDiv)
		var count = 0;
		var t = setInterval(function() {
			loadDiv.style.background = "url(images/loading" + (++count % 3 + 1) + ".png) no-repeat"
			if(count == 11) {
				clearInterval(t)
				logo.remove()
				loadDiv.remove()
				success()
			}
		}, 500)
	}
	self.bgmoving = function() {
		var _top = 0;
		setInterval(function() {
			if(!GameEngine.pause) {
				self.body.style.backgroundPosition = "0px " + (_top += 3) + "px"
			}
		}, 30)
	}
	self.left = function() {
		return self.body.offsetLeft;
	}
	self.width = function() {
		return self.body.offsetWidth
	}
	self.isPengPeng = function(bullet, plane) {
		if(self.isOverlap(bullet, plane)) {
			bullet.explode()
			plane.hurt()
		}
	}
	self.getWeapon = function(myplane, weapon) {
		if(self.isOverlap(myplane, weapon)) {
			weapon.destroy()
			myplane.upgrade()
		}
	}
	self.isOverlap = function(obj1, obj2) {
		var leftSide = obj2.left() - obj1.width()
		var rightSide = obj2.left() + obj2.width()
		var upSide = obj2.top() - obj1.height()
		var downSide = obj2.top() + obj2.height()
		if(obj1.left() > leftSide && obj1.left() < rightSide && obj1.top() > upSide && obj1.top() < downSide) {
			return true
		}
	}
	self.notify = function(bullet) {
		for(var e of self.enemes.keys()) {
			self.isPengPeng(bullet, e)
		}
	}
	self.notify1 = function(myplane) {
		for(var e of self.weapons.keys()) {
			self.getWeapon(myplane, e)
		}
	}
	self.gameover=function(){
		var cover=document.getElementsByClassName("cover")[0]
		GameEngine.pause=!GameEngine.pause;
		cover.children[0].innerText="游戏结束"
		cover.style.display=GameEngine.pause?"block":"none"
	}
}